package com.bdmc.smokesensor.services;

//import java.awt.print.Pageable;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.query.internal.QueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;

@Service
@SuppressWarnings("unchecked")
public class BaseService<T> {

    @PersistenceUnit
    public EntityManagerFactory emf;

    public List<Map<String,Object>> getListMapBySql(String sql){
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query query = em.createNativeQuery(sql);
        query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String,Object>> map = query.getResultList();
        em.close();
        return map;
    }

    public List<Map<String,Object>> getListMapByHql(String sql){
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query query = em.createQuery(sql);
        //query.unwrap(QueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String,Object>> map = query.getResultList();
        em.close();
        return map;
    }

    public List<Map<String,Object>> getListMapBySql(String sql,int page,int rows){
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query query = em.createNativeQuery(sql);
        query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        query.setFirstResult(rows * page);
        query.setMaxResults(rows);
        List<Map<String,Object>> map = query.getResultList();
        em.close();
        return map;
    }

    public List<T> GetListBySql(String sql, Class<T> resultClass) {
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query query = em.createNativeQuery(sql, resultClass);
        List<T> resultList = query.getResultList();
        em.close();
        return resultList;
    }

    @SuppressWarnings("all")
    public List GetListBySql(String sql) {
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query query = em.createNativeQuery(sql);
        query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        em.close();
        return query.getResultList();
    }

    public int GetCountBySql(String sql) {
        EntityManager em = emf.createEntityManager();
        String countSql = "select count(*) from (" + sql + ") tt_count";
        javax.persistence.Query query = em.createNativeQuery(countSql);
        int i = Integer.valueOf(query.getSingleResult().toString());
        em.close();
        return i;
    }

    public Object GetSingleBySql(String sql) {
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query query = em.createNativeQuery(sql);
        Object o = query.getSingleResult().toString();
        em.close();
        return o;
    }

    /**
     * 根据sql返回分页数据，只能返回数据库实体
     */
    public List<T> GetListBySqlPageable(String sql, Class<T> resultClass, int page, int size) {
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query query = em.createNativeQuery(sql, resultClass);
        query.setFirstResult(size * page );
        query.setMaxResults(size);
        List<T> resultList = query.getResultList();
        em.close();
        return resultList;
    }

}